{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "error",
     "evalue": "[Errno 10061] ",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m\u001b[0m",
      "\u001b[1;31merror\u001b[0mTraceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-76d88bc77320>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     19\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     20\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 21\u001b[1;33m     \u001b[0msmtpObj\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msmtplib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSMTP\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'localhost'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     22\u001b[0m     \u001b[0msmtpObj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msendmail\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msender\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreceivers\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmessage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mas_string\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     23\u001b[0m     \u001b[1;32mprint\u001b[0m \u001b[1;34m\"邮件发送成功\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\Program\\Anaconda2\\lib\\smtplib.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, host, port, local_hostname, timeout)\u001b[0m\n\u001b[0;32m    254\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mesmtp_features\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    255\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mhost\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 256\u001b[1;33m             \u001b[1;33m(\u001b[0m\u001b[0mcode\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconnect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhost\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mport\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    257\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mcode\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[1;36m220\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    258\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\Program\\Anaconda2\\lib\\smtplib.py\u001b[0m in \u001b[0;36mconnect\u001b[1;34m(self, host, port)\u001b[0m\n\u001b[0;32m    315\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdebuglevel\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    316\u001b[0m             \u001b[1;32mprint\u001b[0m\u001b[1;33m>>\u001b[0m\u001b[0mstderr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'connect:'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mhost\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mport\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 317\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msock\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_socket\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhost\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mport\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    318\u001b[0m         \u001b[1;33m(\u001b[0m\u001b[0mcode\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetreply\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    319\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdebuglevel\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\Program\\Anaconda2\\lib\\smtplib.py\u001b[0m in \u001b[0;36m_get_socket\u001b[1;34m(self, host, port, timeout)\u001b[0m\n\u001b[0;32m    290\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdebuglevel\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    291\u001b[0m             \u001b[1;32mprint\u001b[0m\u001b[1;33m>>\u001b[0m\u001b[0mstderr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'connect:'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mhost\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mport\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 292\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0msocket\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcreate_connection\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhost\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mport\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    293\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    294\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mconnect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhost\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'localhost'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mport\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\Program\\Anaconda2\\lib\\socket.pyc\u001b[0m in \u001b[0;36mcreate_connection\u001b[1;34m(address, timeout, source_address)\u001b[0m\n\u001b[0;32m    573\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    574\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0merr\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 575\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0merr\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    576\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    577\u001b[0m         \u001b[1;32mraise\u001b[0m \u001b[0merror\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"getaddrinfo returns an empty list\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31merror\u001b[0m: [Errno 10061] "
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: UTF-8 -*-\n",
    " \n",
    "import smtplib\n",
    "from email.mime.text import MIMEText\n",
    "from email.header import Header\n",
    " \n",
    "sender = 'from@runoob.com'\n",
    "receivers = ['429240967@qq.com']  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱\n",
    " \n",
    "# 三个参数：第一个为文本内容，第二个 plain 设置文本格式，第三个 utf-8 设置编码\n",
    "message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8')\n",
    "message['From'] = Header(\"菜鸟教程\", 'utf-8')   # 发送者\n",
    "message['To'] =  Header(\"测试\", 'utf-8')        # 接收者\n",
    " \n",
    "subject = 'Python SMTP 邮件测试'\n",
    "message['Subject'] = Header(subject, 'utf-8')\n",
    " \n",
    " \n",
    "try:\n",
    "    smtpObj = smtplib.SMTP('localhost')\n",
    "    smtpObj.sendmail(sender, receivers, message.as_string())\n",
    "    print \"邮件发送成功\"\n",
    "except smtplib.SMTPException:\n",
    "    print \"Error: 无法发送邮件\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !/usr/bin/env python\n",
    "# -*- coding:utf-8 -*-\n",
    "\n",
    "\n",
    "# 可行\n",
    "\n",
    "import smtplib\n",
    "from email.header import Header\n",
    "from email.mime.text import MIMEText\n",
    "from email.utils import parseaddr,formataddr\n",
    " \n",
    " \n",
    "# 163邮箱服务器地址\n",
    "smtp_server='smtp.163.com'\n",
    "# 发件人邮箱地址\n",
    "from_addr='jiangcheng1806@163.com'\n",
    "# 发件人邮箱密码(使用申请的客户端授权码代替)\n",
    "passwd='13685541806'\n",
    "# 收件人邮箱地址\n",
    "to_addr='969032241@qq.com'\n",
    " \n",
    "# 构建邮件内容(参数:邮件内容; 类型-plain,html; 编码)\n",
    "msg=MIMEText('SMTP测试邮件','plain','utf-8')\n",
    "# 设置邮件主题\n",
    "msg['Subject']=Header('这是邮件主题',charset='utf-8')\n",
    " \n",
    "def send_email():\n",
    "    # 获取SMTP对象\n",
    "    server=smtplib.SMTP(host=smtp_server,port=25)\n",
    "    # 登录163邮箱服务器\n",
    "    server.login(user=from_addr,password=passwd)\n",
    "    # 发送邮件\n",
    "    server.sendmail(from_addr=from_addr,to_addrs=to_addr,msg=msg.as_string())\n",
    "    # 退出邮箱服务器\n",
    "    server.quit()\n",
    " \n",
    " \n",
    "if __name__ == '__main__':\n",
    "    send_email()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(535, 'Error: \\xc7\\xeb\\xca\\xb9\\xd3\\xc3\\xca\\xda\\xc8\\xa8\\xc2\\xeb\\xb5\\xc7\\xc2\\xbc\\xa1\\xa3\\xcf\\xea\\xc7\\xe9\\xc7\\xeb\\xbf\\xb4: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256')\n",
      "邮件发送失败\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: UTF-8 -*-\n",
    " \n",
    "import smtplib\n",
    "from email.mime.text import MIMEText\n",
    "from email.utils import formataddr\n",
    " \n",
    "my_sender='969032241@qq.com'    # 发件人邮箱账号\n",
    "my_pass = '(jcgz2+@714082)'              # 发件人邮箱密码\n",
    "my_user='jiangcheng1806@163.com'      # 收件人邮箱账号，我这边发送给自己\n",
    "def mail():\n",
    "    ret=True\n",
    "    try:\n",
    "        msg=MIMEText('填写邮件内容','plain','utf-8')\n",
    "        msg['From']=formataddr([\"FromRunoob\",my_sender])  # 括号里的对应发件人邮箱昵称、发件人邮箱账号\n",
    "        msg['To']=formataddr([\"FK\",my_user])              # 括号里的对应收件人邮箱昵称、收件人邮箱账号\n",
    "        msg['Subject']=\"菜鸟教程发送邮件测试\"                # 邮件的主题，也可以说是标题\n",
    " \n",
    "        server=smtplib.SMTP_SSL(\"smtp.qq.com\", 465)  # 发件人邮箱中的SMTP服务器，端口是25\n",
    "        server.login(my_sender, my_pass)  # 括号中对应的是发件人邮箱账号、邮箱密码\n",
    "        server.sendmail(my_sender,[my_user,],msg.as_string())  # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件\n",
    "        server.quit()  # 关闭连接\n",
    "    except Exception as e:  # 如果 try 中的语句没有执行，则会执行下面的 ret=False\n",
    "        print e\n",
    "        ret=False\n",
    "    return ret\n",
    " \n",
    "ret=mail()\n",
    "if ret:\n",
    "    print(\"邮件发送成功\")\n",
    "else:\n",
    "    print(\"邮件发送失败\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "ename": "SMTPServerDisconnected",
     "evalue": "Connection unexpectedly closed: [Errno 10054] ",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m\u001b[0m",
      "\u001b[1;31mSMTPServerDisconnected\u001b[0mTraceback (most recent call last)",
      "\u001b[1;32m<ipython-input-14-fed2fbfdfb37>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     37\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     38\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 39\u001b[1;33m     \u001b[0msend_email\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-14-fed2fbfdfb37>\u001b[0m in \u001b[0;36msend_email\u001b[1;34m()\u001b[0m\n\u001b[0;32m     29\u001b[0m     \u001b[0mserver\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msmtplib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSMTP\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhost\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0msmtp_server\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mport\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m25\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     30\u001b[0m     \u001b[1;31m# 登录163邮箱服务器\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 31\u001b[1;33m     \u001b[0mserver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlogin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0muser\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfrom_addr\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mpassword\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mpasswd\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     32\u001b[0m     \u001b[1;31m# 发送邮件\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     33\u001b[0m     \u001b[0mserver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msendmail\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfrom_addr\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfrom_addr\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mto_addrs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mto_addr\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mmsg\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmsg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mas_string\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\Program\\Anaconda2\\lib\\smtplib.py\u001b[0m in \u001b[0;36mlogin\u001b[1;34m(self, user, password)\u001b[0m\n\u001b[0;32m    581\u001b[0m         \u001b[0mAUTH_LOGIN\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"LOGIN\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    582\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 583\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mehlo_or_helo_if_needed\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    584\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    585\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhas_extn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"auth\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\Program\\Anaconda2\\lib\\smtplib.py\u001b[0m in \u001b[0;36mehlo_or_helo_if_needed\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    541\u001b[0m         \"\"\"\n\u001b[0;32m    542\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhelo_resp\u001b[0m \u001b[1;32mis\u001b[0m \u001b[0mNone\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mehlo_resp\u001b[0m \u001b[1;32mis\u001b[0m \u001b[0mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 543\u001b[1;33m             \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m200\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mehlo\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[1;36m299\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    544\u001b[0m                 \u001b[1;33m(\u001b[0m\u001b[0mcode\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresp\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhelo\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    545\u001b[0m                 \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m200\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[0mcode\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[1;36m299\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\Program\\Anaconda2\\lib\\smtplib.py\u001b[0m in \u001b[0;36mehlo\u001b[1;34m(self, name)\u001b[0m\n\u001b[0;32m    413\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mesmtp_features\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    414\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mputcmd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mehlo_msg\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlocal_hostname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 415\u001b[1;33m         \u001b[1;33m(\u001b[0m\u001b[0mcode\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetreply\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    416\u001b[0m         \u001b[1;31m# According to RFC1869 some (badly written)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    417\u001b[0m         \u001b[1;31m# MTA's will disconnect on an ehlo. Toss an exception if\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\Program\\Anaconda2\\lib\\smtplib.py\u001b[0m in \u001b[0;36mgetreply\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    364\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    365\u001b[0m                 raise SMTPServerDisconnected(\"Connection unexpectedly closed: \"\n\u001b[1;32m--> 366\u001b[1;33m                                              + str(e))\n\u001b[0m\u001b[0;32m    367\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mline\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m''\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    368\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mSMTPServerDisconnected\u001b[0m: Connection unexpectedly closed: [Errno 10054] "
     ]
    }
   ],
   "source": [
    "# !/usr/bin/env python\n",
    "# -*- coding:utf-8 -*-\n",
    "\n",
    "\n",
    "# 可行\n",
    "\n",
    "import smtplib\n",
    "from email.header import Header\n",
    "from email.mime.text import MIMEText\n",
    "from email.utils import parseaddr,formataddr\n",
    " \n",
    " \n",
    "# 163邮箱服务器地址\n",
    "smtp_server='smtp.163.com'\n",
    "# 发件人邮箱地址\n",
    "from_addr='jiangcheng1806@163.com'\n",
    "# 发件人邮箱密码(使用申请的客户端授权码代替)\n",
    "passwd='13685541806'\n",
    "# 收件人邮箱地址\n",
    "to_addr='969032241@qq.com'\n",
    " \n",
    "# 构建邮件内容(参数:邮件内容; 类型-plain,html; 编码)\n",
    "msg=MIMEText('SMTP测试邮件','html','utf-8')\n",
    "# 设置邮件主题\n",
    "msg['Subject']=Header('这是邮件主题',charset='utf-8')\n",
    " \n",
    "def send_email():\n",
    "    # 获取SMTP对象\n",
    "    server=smtplib.SMTP(host=smtp_server,port=25)\n",
    "    # 登录163邮箱服务器\n",
    "    server.login(user=from_addr,password=passwd)\n",
    "    # 发送邮件\n",
    "    server.sendmail(from_addr=from_addr,to_addrs=to_addr,msg=msg.as_string())\n",
    "    # 退出邮箱服务器\n",
    "    server.quit()\n",
    " \n",
    " \n",
    "if __name__ == '__main__':\n",
    "    send_email()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
